﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;

namespace FacebookApp
{
    public class OrderService
	{

		#region[Order_GetbyAll]
		public static List<Order> Order_GetByAll()
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    var da = (from au in dt.Orders select au).ToList();

                    return da;
                }
            }
            catch (Exception)
            {
                return null;
            }
		}
		#endregion
		#region[Order_GetbyId]
        public static Order Order_GetById(string Data)
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(Data);
                    var da = (from au in dt.Orders where au.Id == id select au).First();

                    return da;
                }
            }
            catch (Exception)
            {
                return null;
            }
		}
		#endregion
		#region[Order_GetbyTop]
        public static List<Order> Order_GetByTop(string Top, string Where, string Order)
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    string query = "Select * from [Order]";
                    if (!Top.Equals(""))
                        query = " Select top " + Top + " * from [Order] ";
                    if (!Where.Equals(""))
                        query += " where " + Where + " ";
                    if (!Order.Equals(""))
                        query += " order by " + Order;
                    
                    IEnumerable<Order> results = dt.ExecuteQuery<Order>(@query);

                    return results.ToList();
                }
            }
            catch (Exception e)
            {
                return null;
            }
		}
		#endregion
		#region[Order_Insert]
        public static string Order_Insert(OrderInfo Data)
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {

                    Order ob = new Order();
                    ob.FB_Id = Data.FB_Id;
                    ob.Date = System.DateTime.Now;                    
                    ob.TotalMoney = decimal.Parse(Data.TotalMoney);                    
                    ob.Status = int.Parse(Data.Status);//dang kiem tra
                    ob.Address = Data.Address;
                    ob.Phone = Data.Phone;                    
                    ob.FullName = Data.FullName;
                    ob.Email = Data.Email;
                    ob.GroupUser_Id = long.Parse(Data.GroupUser_Id);
                    dt.Orders.InsertOnSubmit(ob);
                    dt.SubmitChanges();

                    //tra ve id vua insert
                    string query = "Select Top 1 Id From [Order] Order by Id DESC";
                    IEnumerable<long> results = dt.ExecuteQuery<long>(@query);
                    return results.First().ToString();
                }
            }
            catch (Exception ex)
            {
                //WebMsgBox.Show(ex.Message);
                return "-1";
            }
		}
		#endregion
		#region[Order_Update]
        public static void Order_Update(OrderInfo Data)
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(Data.Id);
                    //Author da = (from au in dt.Authors where au.Id == id select au).First();
                    Order da = dt.Orders.Single(au => au.Id == id);
                    da.FB_Id = Data.FB_Id;
                    //da.Date = System.DateTime.Now;//khong cap nhat lai thoi gian
                    da.TotalMoney = decimal.Parse(Data.TotalMoney);
                    da.Status = int.Parse(Data.Status);//0 -dang kiem tra, 1 - dang giao dich, 2-da thanh toan
                    da.Address = Data.Address;
                    da.Phone = Data.Phone;
                    da.FullName = Data.FullName;
                    da.Email = Data.Email;

                    dt.SubmitChanges();
                }
            }
            catch (Exception)
            {

            }
		}
		#endregion
		#region[Order_Delete]
        public static void Order_Delete(string Data)
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(Data);
                    //Author da = (from au in dt.Authors where au.Id == id select au).First();
                    Order da = dt.Orders.Single(au => au.Id == id);
                    dt.Orders.DeleteOnSubmit(da);

                    dt.SubmitChanges();
                }
            }
            catch (Exception)
            {

            }
		}
		#endregion
        #region[Order_UpdateStatus]
        public static void Order_UpdateStatus(string Id,string status)
        {
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(Id);
                    //Author da = (from au in dt.Authors where au.Id == id select au).First();
                    Order da = dt.Orders.Single(au => au.Id == id);
                    da.Status = int.Parse(status);//0 -dang kiem tra, 1 - dang giao dich, 2-da thanh toan                    

                    dt.SubmitChanges();
                }
            }
            catch (Exception)
            {

            }
        }
        #endregion
	}

    public class OrderInfo
	{
		public string Id { get; set; }
		public string FB_Id { get; set; }
		public string Date { get; set; }
		public string TotalMoney { get; set; }
		public string Status { get; set; }
		public string Address { get; set; }
		public string Phone { get; set; }
		public string FullName { get; set; }
        public string Email { get; set; }
        public string GroupUser_Id { get; set; }

	}
}

